/*
 * 2007/05
 * 2007/10
 * 2011/11/17 - 13:30
 */
package org.nettyflux.example.asteroids;

import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 * @author Victor Williams Stafusa da Silva
 */
public class MainWindow extends JFrame {

    private static final long serialVersionUID = 7929597905942411867L;

    private static final int SCREEN_WIDTH = 400;
    private static final int SCREEN_HEIGHT = 200;

    private RenderScreen innerArea;
    private boolean closed = false;

    private WindowAdapter wa = new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            closeWindow();
        }

        @Override
        public void windowClosed(WindowEvent e) {
            closeWindow();
        }
    };

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainWindow();
            }
        });
    }

    public MainWindow() {
        this.setLayout(null);
        this.setSize(450, 250);
        innerArea = new RenderScreen(SCREEN_WIDTH, SCREEN_HEIGHT);
        this.add(innerArea, BorderLayout.CENTER);
        this.addKeyListener(kl);
        this.addWindowListener(wa);
        this.setTitle("Ping-Pong! - Victor");
        this.setResizable(false);
        this.setVisible(true);
        innerArea.initSize();
        Insets insets = this.getInsets();
        this.setSize(SCREEN_WIDTH + insets.left + insets.right, SCREEN_HEIGHT + insets.top + insets.bottom);
        innerArea.start();
    }

    private KeyListener kl = new KeyListener() {
        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                case KeyEvent.VK_W:
                    innerArea.paddleGoUp(true);
                    break;

                case KeyEvent.VK_DOWN:
                case KeyEvent.VK_X:
                    innerArea.paddleGoDown(true);
                    break;

                case KeyEvent.VK_ESCAPE:
                    closeWindow();
                    break;
            }
            e.consume();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_UP:
                case KeyEvent.VK_W:
                    innerArea.paddleGoUp(false);
                    break;

                case KeyEvent.VK_DOWN:
                case KeyEvent.VK_X:
                    innerArea.paddleGoDown(false);
                    break;
            }
            e.consume();
        }

        @Override
        public void keyTyped(KeyEvent e) {
            e.consume();
        }
    };

    private void closeWindow() {
        if (closed) return;
        closed = true;
        innerArea.close();
        this.dispose();
    }
}